package org.example.smartEducationBackend.service;

import com.aliyun.oss.OSS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.example.smartEducationBackend.entity.DTO.UserDTO;
import org.example.smartEducationBackend.entity.POJO.UserAccount;
import org.example.smartEducationBackend.entity.POJO.UserVideoWatch;
import org.example.smartEducationBackend.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class UserService {
    @Autowired
    private OSS ossClient;
    
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    
    @Autowired
    UserMapper userMapper;
    public Map<String,Object> getUser(UserDTO userDTO) {
        UserDTO user = userMapper.selectByUser(userDTO);
        HashMap<String, Object> map = new HashMap<>();

        map.put("user",user);
        return map;
    }

    public UserAccount getUserById(Integer userid) {
        UserAccount userAccount = userMapper.selectById(userid);

        return userAccount;
    }

    public String saveUserAvatar(MultipartFile file) {
        try {
            // 1. 验证文件类型
            String originalFilename = file.getOriginalFilename();
            String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));
            if (!isImageFile(fileType)) {
                throw new RuntimeException("不支持的文件类型");
            }

            // 2. 生成唯一文件名
            String fileName = "avatar/" + "userAvatar" + "/" + UUID.randomUUID() + fileType;

            // 3. 上传到OSS
            ossClient.putObject(bucketName, fileName, file.getInputStream());

            // 4. 生成访问URL
            String avatarUrl = "https://" + bucketName + "." + endpoint + "/" + fileName;


            return avatarUrl;
        } catch (Exception e) {
            throw new RuntimeException("上传头像失败: " + e.getMessage());
        }
    }

    private boolean isImageFile(String fileType) {
        String[] allowedTypes = {".jpg", ".jpeg", ".png", ".gif"};
        for (String type : allowedTypes) {
            if (type.equalsIgnoreCase(fileType)) {
                return true;
            }
        }
        return false;
    }

    public Integer update(UserAccount userAccount) {

        UpdateWrapper<UserAccount> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",userAccount.getUserId());

        int update = userMapper.update(userAccount,updateWrapper);
        return update;
    }

    public UserAccount getuserByName(String username) {
        QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);

        UserAccount userAccount = userMapper.selectOne(queryWrapper);
        return userAccount;
    }

    public void insert(UserAccount userAccount) {
        userAccount.setRegisterTime(new Date());
        userMapper.insert(userAccount);
    }
}
